﻿using System;
using System.Web;
using Wicresoft.RDW.Helper;
using Wicresoft.RDW.Model;
using Wicresoft.RDW.Presentation;
using System.Web.UI.WebControls;
using Resources;

namespace Wicresoft.RDW.Upload
{
    public partial class Upload : BasePage_BackEnd
    {
        private const string PictureId = "Id";
        private static int updatePicId = 0;
        private static bool status = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            if (!IsPostBack)
            {
                status = false;
                InitControlResource();
                pager.CurrentPageIndex = 0;
                PagerControll_PageChange(pager.CurrentPageIndex);
                
            }

        }

        protected void PagerControll_PageChange(int pageIndex)
        {
            string language = Language.ToString();
            var dataPagerSource = PerfectPicturePresent.GetPaged(pageIndex, pager.PageSize, p => true,p=>p.CreatTime,true);
            gvPerfectPicture.DataSource = dataPagerSource;
            gvPerfectPicture.DataBind();
            var count = 0;
            count = dataPagerSource.TotalCount;
            pager.IntialProperties(count);
            lbtnStatus(status);
        }

        public override void InitControlResource()
        {
            int i = 0;
            gvPerfectPicture.Columns[i++].HeaderText = NameResource.No;
            gvPerfectPicture.Columns[i++].HeaderText = NameResource.PerfectPicture_Pic;
            gvPerfectPicture.Columns[i++].HeaderText = NameResource.PerfectPicture_DescCN;
            gvPerfectPicture.Columns[i++].HeaderText = NameResource.PerfectPicture_DescEN;
            gvPerfectPicture.Columns[i++].HeaderText = ManageResource.mgr_lblAction;
            lblDescCN.Text = NameResource.PerfectPicture_DescCN;
            lblDescEN.Text = NameResource.PerfectPicture_DescEN;
            lblEditDescCN.Text = NameResource.PerfectPicture_DescCN;
            lblEditDescEN.Text = NameResource.PerfectPicture_DescEN;
            lblUploadPic.Text = ManageResource.mgr_lblUploadPicture;
            btnUpload.Text = ManageResource.mgr_btnOK;
            btnCancel.Text = ManageResource.mgr_btnCancel;
            lbtnSubmitEdit.Text = ManageResource.mgr_btnOK;
            lbtnCancelEdit.Text = ManageResource.mgr_btnCancel;
            btnAdd.Text = ManageResource.mgr_btnAddPicture;
            rfvtxtDescCN.ErrorMessage = NameResource.PerfectPicture_DescCN + CommonResource.NotNull;
            rfvtxtDescEN.ErrorMessage = NameResource.PerfectPicture_DescEN + CommonResource.NotNull;
            rfvtxtEditDescCN.ErrorMessage = NameResource.PerfectPicture_DescCN + CommonResource.NotNull;
            rfvtxtEditDescEN.ErrorMessage = NameResource.PerfectPicture_DescEN + CommonResource.NotNull;
            lblPicture.Text = NameResource.PerfectPicture_Pic;
        }

        protected void gvPerfectPicture_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = 0;
                e.Row.Cells[i++].Text = (e.Row.RowIndex + 1 + (pager.CurrentPageIndex) * pager.PageSize).ToString();
                var lbtnDelete = e.Row.Cells[3].FindControl("lbtnDelete") as LinkButton;
                var lbtnUpdate = e.Row.Cells[3].FindControl("lbtnUpdate") as LinkButton;
                lbtnDelete.Text = ManageResource.mgr_btnDelete;
                lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                lbtnUpdate.Text = ManageResource.mgr_btnEdit;
                
            }
        }

        protected void gvPerfectPicture_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            int pictureId = (int)this.gvPerfectPicture.DataKeys[i][PictureId];
            PerfectPicturePresent.Delete(pictureId);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void gvPerfectPicture_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            updatePicId = (int)this.gvPerfectPicture.DataKeys[i][PictureId];
            phEdit.Visible = true;
            phUpload.Visible = false;
            lbtnStatus(true);
            status = true;
            var perfectPic = PerfectPicturePresent.GetById(updatePicId);
            if (perfectPic != null)
            {
                txtEditDescCN.Text = Server.HtmlDecode(perfectPic.Desc_CN);
                txtEditDescEN.Text = Server.HtmlDecode(perfectPic.Desc_EN);
                imgEdit.Src = perfectPic.Path;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            txtDescCN.Text = string.Empty;
            txtDescEN.Text = string.Empty;
            phUpload.Visible = true;
            phEdit.Visible = false;
            lblUploadError.Text = string.Empty;
            lbtnStatus(true);
            status = true;
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Picture.FileName) == false)
            {
                HttpPostedFile file = this.Request.Files[0];
                PerfectPicture perfectPicture = new PerfectPicture();
                perfectPicture.Desc_CN = Server.HtmlEncode(txtDescCN.Text);
                perfectPicture.Desc_EN = Server.HtmlEncode(txtDescEN.Text);
                perfectPicture.CreateBy = CurrentUserId;
                perfectPicture.CreatTime = DateTime.Now;
                string fileExt = System.IO.Path.GetExtension(Picture.FileName).ToLower();
                PerfectPictureOperator uploadFile = new PerfectPictureOperator();
                string mapPath = this.Context.Server.MapPath("/UploadImages");
                string errorstr = string.Empty;
                if (uploadFile.Upload(file, mapPath, perfectPicture, out errorstr))
                {
                    pager.CurrentPageIndex = 0;
                    PagerControll_PageChange(pager.CurrentPageIndex);
                    phUpload.Visible = false;
                    lbtnStatus(false);
                    status = false;
                }
                else
                {
                    lblUploadError.Text = NameResource.UploadPicture_Fail + errorstr;
                }
            }
            else
            {
                lblUploadError.Text = NameResource.PleaseSelectPicture;
            }
        }

        protected void lbtnSubmitEdit_Click(object sender, EventArgs e)
        {
            phEdit.Visible = false;
            lbtnStatus(false);
            status = false;
            var updatePerfectPicture = PerfectPicturePresent.GetById(updatePicId);
            updatePerfectPicture.Desc_CN = Server.HtmlEncode(txtEditDescCN.Text);
            updatePerfectPicture.Desc_EN = Server.HtmlEncode(txtEditDescEN.Text);
            PerfectPicturePresent.Update(updatePerfectPicture);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            phUpload.Visible = false;
            phEdit.Visible = false;
            lbtnStatus(false);
            status = false;
        }

        protected void lbtnStatus(bool status)
        {
            int count1 = gvPerfectPicture.Rows.Count;
            
            if (status)
            {
                for (int i = 0; i < count1; i++)
                {
                    LinkButton lbtnDelete = gvPerfectPicture.Rows[i].FindControl("lbtnDelete") as LinkButton;
                    lbtnDelete.Attributes.Add("disabled", "disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return false;");
                    LinkButton lbtnUpdate = gvPerfectPicture.Rows[i].FindControl("lbtnUpdate") as LinkButton;
                    lbtnUpdate.Attributes.Add("disabled", "disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return false;");
                }
            }
            else
            {
                for (int i = 0; i < count1; i++)
                {
                    int pictureId = (int)this.gvPerfectPicture.DataKeys[i][PictureId];
                    var perfectPicture = PerfectPicturePresent.GetById(pictureId);
                    LinkButton lbtnDelete = gvPerfectPicture.Rows[i].FindControl("lbtnDelete") as LinkButton;
                    lbtnDelete.Attributes.Remove("disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                    LinkButton lbtnUpdate = gvPerfectPicture.Rows[i].FindControl("lbtnUpdate") as LinkButton;
                    lbtnUpdate.Attributes.Remove("disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return true;");
                    if (IsLabManager&CurrentUserId != perfectPicture.CreateBy)
                    {
                        lbtnDelete.Attributes.Add("disabled", "disabled");
                        lbtnDelete.Attributes.Remove("onclick");
                        lbtnDelete.Attributes.Add("onclick", "return false;");
                        lbtnUpdate.Attributes.Add("disabled", "disabled");
                        lbtnUpdate.Attributes.Remove("onclick");
                        lbtnUpdate.Attributes.Add("onclick", "return false;");
                    }
                }
            }
        }
    }
}